package com.numberONe.controller.illness;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.numberONe.controller.index.BaseController;
import com.numberONe.entity.SeriesFormMap;
import com.numberONe.entity.SymptomFormMap;
import com.numberONe.mapper.SeriesMapper;
import com.numberONe.mapper.SymptomMapper;
import com.numberONe.util.Common;
import com.numberONe.util.ExcelUtil;

@Controller
@RequestMapping("/illness")
public class IllnessController extends BaseController {

	@Inject
	private SeriesMapper SeriesMapper;
	@Inject
	private SymptomMapper symptomMapper;
	
	@RequestMapping("/forimp")
	public String forimp() {
		return Common.WEB_PATH + "/impdata/imp";
	}
	
	@ResponseBody
	@RequestMapping("/impdata")
	public List<List<String>> impdata(@RequestParam(value="file",required=false) MultipartFile file) throws IOException {
        List<String> types = Arrays.asList(".xls",".xlsx");
        String fileName = file.getOriginalFilename();
        
        // 代表当前处理的item里面封装的是上传文件
        //String fileName = item.getName().substring(item.getName().lastIndexOf("\\")+1); 
        // 拿到文件的扩展名
        String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();  
        if (!types.contains(ext)) {
        	//return "本系统不支持" + ext + "这种类型";
        }
        InputStream in = file.getInputStream();
        List<List<List<String>>> data = null;
        if(".xls".equals(ext)) {
        	data = ExcelUtil.xlsreader(in);
        }else if(".xlsx".equals(ext)) {
        	data = ExcelUtil.xlsxreader(in);
        }
        if(data.size()>1) {
        	//return "模板不正确！";
        }
        List<List<String>> sheet = data.get(0);
        //List<SeriesFormMap> series = SeriesMapper.findByAttribute(key, value, clazz)
        return sheet;
	}

	@RequestMapping(value = "/addcase")
	public String addcase(Map<String, Object> map) {
		List<SeriesFormMap> illnesstype = SeriesMapper.findByAttribute("level", "0", SeriesFormMap.class);
		String no = illnesstype.get(0).getStr("no");
		List<SeriesFormMap> series = SeriesMapper.findByAttribute("pno", no, SeriesFormMap.class);
		map.put("illnesstype", illnesstype);
		map.put("series", series);
		return Common.WEB_PATH + "/case/caseinput";
	}
	
	@ResponseBody
	@RequestMapping("/findseries")
	public List<SeriesFormMap> findseries(HttpServletRequest req){
		String no = req.getParameter("no");
		List<SeriesFormMap> series = SeriesMapper.findByAttribute("pno",no,SeriesFormMap.class);
		return series;
	}

	@RequestMapping(value = "/findSymptomBySeriesId")
	public String findSymptomBySeriesId(Map<String, Object> map) {
		String type_no = "'"+getParaValues("type_no")[0].replaceAll(",", "','")+"'";
		map.put("type_no", type_no);
		List<SymptomFormMap> list = symptomMapper.findSymptomBySeriesId(map);
		map.put("symptoms", list);
		return Common.WEB_PATH + "/case/symptionsel";
	}

}
